USE [kymedi2b2data]
GO

/****** Object:  UserDefinedFunction [dbo].[MOD_CONSTRAINT]    Script Date: 10/22/2012 3:09:23 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE FUNCTION [dbo].[MOD_CONSTRAINT] (@MODIFIER_CD INT, @modifier_dim varchar(700))
RETURNS VARCHAR(MAX)
AS
BEGIN

	declare @sql varchar(max) 
	
	set @sql = case
				   when @MODIFIER_CD IS NULL
				   then ''
				   when @MODIFIER_CD IS NOT NULL
				   then case when @modifier_dim like 'encounter_num%' and @MODIFIER_CD = 0
							 then ltrim(@modifier_dim)+' '
							 when @modifier_dim like 'encounter_num%' and @MODIFIER_CD = 1
							 then ltrim(@modifier_dim)+' AND ' + 'MODIFIER_CD = '+cast(@modifier_cd as varchar(2))+' ' 
							 else 'MODIFIER_CD = '+cast(@modifier_cd as varchar(1))+' '
							 end
			   end + char(13)
	RETURN @sql
END

GO


